var url = 'ws://192.168.0.195:8291'; //服务器地址

function connect(userid, func) {
	var userid = wx.getStorageSync('userInfo').user_id
	console.log(userid)
	wx.connectSocket({
		url: url,
		header: {
			'content-type': 'application/json',
			'access-token': userid
		},
		success: function() {
			console.log('websocket连接成功~')
		},
		fail: function() {
			console.log('websocket连接失败~')
		}
	})
	wx.onSocketOpen(function(res) {
		wx.showToast({
			title: 'websocket已开通~',
			icon: "success",
			duration: 2000
		})
		//接受服务器消息
		wx.onSocketMessage(function (res) {
            console.log('收到服务器内容：', res);
          });
	});
	wx.onSocketError(function(res) {
		wx.showToast({
			title: 'websocket连接失败，请检查！',
			icon: "none",
			duration: 2000
		})
	});
}
//发送消息
function send(msg) {

	return new Promise((res,req)=>{
		wx.sendSocketMessage({
			data: msg,
			success:data=>{
				console.log(data)
				res(data)
			}

		});
	})
}
module.exports = {
	connect: connect,
	send: send,
}
